package com.crm.web.controller.clue;


import com.crm.common.annotation.Log;
import com.crm.common.core.controller.BaseController;


import com.crm.common.core.domain.AjaxResult;
import com.crm.common.core.page.TableDataInfo;
import com.crm.common.enums.BusinessType;
import com.crm.domain.Contract;
import com.crm.service.ContractService;
import com.crm.service.CourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 合同Controller
 */
@RestController
@RequestMapping("/contract")
public class ContractController extends BaseController {
    @Autowired
    private ContractService contractService;
    @Autowired
    private CourseService courseService;

    /**
     * 查询合同列表
     */
    @PreAuthorize("@ss.hasPermi('contract:list:change')")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract) {
        List<Contract> contractList = contractService.selectContract(contract);
        return getDataTable(contractList);
    }

    /**
     * 新增合同
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:add')")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contract contract){
        if(courseService.checkCluePhoneExis(contract.getPhone())){
            return error("手机号已存在");
        }
        return toAjax(contractService.insertContract(contract));
    }

//    /**
//     * 修改合同
//     */
//    @ApiOperation("修改合同")
//    @PreAuthorize("@ss.hasPermi('contract:contract:edit')")
//    @Log(title = "合同", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody Contract contract)
//    {
//        return toAjax(contractService.updateContract(contract));
//    }

    /**
     * 商机转合同
     * @param id
     * @param contract
     * @return
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:change')")
    @Log(title = "商机转合同", businessType = BusinessType.UPDATE)
    @PutMapping("/changeContract/{id}")
    public AjaxResult changeContract(@PathVariable Long id, @RequestBody Contract contract){
        return toAjax(contractService.changeContract(id,contract));
    }

    /**
     * 获取合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:detail')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id)
    {
        return AjaxResult.success(contractService.selectContractById(id));
    }

    /**
     * 根据id获取合同详细信息
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:detail')")
    @GetMapping(value = "/{id}")
    public AjaxResult detailById(@PathVariable("id") Long id){
        return AjaxResult.success(contractService.selectContractById(id));
    }

}